package com.example.demo;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.*;

@WebServlet(name = "regservlet", value = "/regservlet")
public class regservlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String name = request.getParameter("txtname");
        String pwd= request.getParameter("txtpwd");
        String sex= request.getParameter("sex");
        String email = request.getParameter("txtemail");
        String phone = request.getParameter("txtphone");
        String resume = request.getParameter("txtresume");
        System.out.println("name"+name);

        Connection conn = null;
        Statement st = null;
        try{
            //172.23.79.66
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://172.18.64.254:3306/stu","webdb","webdb");
            String sql = " insert into memberinfo(mName,mpwd,msex,memail,mphone,mresume) values('"+name+"','"+pwd+"','"+sex+"','"+email+"','"+phone+"','"+resume+"')";
            st = conn.createStatement();
            int ret = st.executeUpdate(sql);
            if(ret == 1)
            {
                response.getWriter().println("注册成功了");
            }
        }catch (ClassNotFoundException ex){
            System.out.println("没有找到驱动");
        } catch (SQLException e) {
            response.getWriter().println("注册失败");
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        finally {
            try{
                if(st != null) st.close();
                if(conn != null) conn.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
}
